package com.chenzhou.dao;

import com.chenzhou.common.BaseDAO;
import com.chenzhou.entity.GoodsEntity;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class GoodsDAOImpl extends BaseDAO<GoodsEntity> {

    @Override
    protected List<GoodsEntity> resultToList(ResultSet rs) throws SQLException {
        List<GoodsEntity> list = new ArrayList<>();
        while(rs.next()){
            GoodsEntity goodsEntity = new GoodsEntity();
            goodsEntity.setId(rs.getInt("id"));
            goodsEntity.setGname(rs.getString("gname"));
            goodsEntity.setGoodsInfo(rs.getString("goodsInfo"));
            goodsEntity.setPrice(rs.getFloat("price"));
            goodsEntity.setNum(rs.getInt("num"));
            goodsEntity.setGoods_deltis(rs.getString("goods_deltis"));
            goodsEntity.setGoods_type(rs.getInt("goods_type"));
            goodsEntity.setMarkup(rs.getString("markup"));
            goodsEntity.setType_name(rs.getString("type_name"));
            goodsEntity.setStatus(rs.getInt("status"));
            list.add(goodsEntity);
        }
        return list;
    }

    public List<GoodsEntity> queryAllGoodsEntity(){
        String sql = "SELECT g.*,gt.type_name from tbl_goods g " +
                "left join tbl_goods_type gt on g.goods_type=gt.id";
        return this.execute_Query(sql,null);
    }

    /***
     * 根据状态码查询商品信息
     * @param status
     * @return
     */
    public List<GoodsEntity> queryGoodsEntityBystatus(Integer status){
        String sql = "SELECT g.*,gt.type_name from tbl_goods g " +
                "left join tbl_goods_type gt on g.goods_type=gt.id where status = ?";
        Object[] objects = new Object[]{status};
        return this.execute_Query(sql,objects);
    }

    /***
     * 修改商品上下架
     * @param id
     * @param status
     * @return
     */
    public int modify_goodsStatus(Integer id,Integer status){
        String sql = "update tbl_goods set status=? where id=?";
        Object[] objects = new Object[]{status,id};
        return this.execute_Modify(sql,objects);
    }

    public GoodsEntity queryGoodsEntityByID(Integer id){
        String sql = "SELECT g.*,gt.type_name from tbl_goods g " +
                "left join tbl_goods_type gt on g.goods_type=gt.id where g.id=?";
        Object[] objects = new Object[]{id};
        List<GoodsEntity> list = this.execute_Query(sql,objects);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    public int add_GoodsEntity(GoodsEntity goodsEntity){
        String sql = "insert into tbl_goods values(null,?,?,?,?,?,?,?,?)";
        Object[] objects = new Object[]{
                goodsEntity.getGname(),
                goodsEntity.getGoodsInfo(),
                goodsEntity.getPrice(),
                goodsEntity.getNum(),
                goodsEntity.getGoods_deltis(),
                goodsEntity.getGoods_type(),
                goodsEntity.getMarkup(),
                new Integer(0)
        };

        return this.execute_Modify(sql,objects);
    }

    public int update_GoodsEntity(GoodsEntity goodsEntity){
        String sql = "update tbl_goods set gname=?,goodsInfo=?" +
                ",price=?,num=?,goods_deltis=?,goods_type=?,markup=? where id=?";
        Object[] objects = new Object[]{
                goodsEntity.getGname(),
                goodsEntity.getGoodsInfo(),
                goodsEntity.getPrice(),
                goodsEntity.getNum(),
                goodsEntity.getGoods_deltis(),
                goodsEntity.getGoods_type(),
                goodsEntity.getMarkup(),
                goodsEntity.getId()
        };

        return this.execute_Modify(sql,objects);
    }

    public int delete_GoodsEntity(Integer id){
        String sql = "delete from tbl_goods where id = ?";
        Object[] objects = new Object[]{id};
        return this.execute_Modify(sql,objects);
    }

}
